home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
libgutil
/
filepath.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
162 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* filepath -
* pathname convenience package
*
*/
#include "filepath.h"
#include <string.h>
#include <stdio.h>
#include <pwd.h>
#include <stdlib.h>
char *mymalloc();
char*
pathbasename(char* path)
{
char* c = path;
char* lastbase = 0;
int length = 0;
char* retstr;
if (!path)
return 0;
if (*c != '/') { /* first name might be base */
lastbase = c;
}
while (*c) {
if (*c == '/') {
switch (*(c+1)) { /* look-ahead one char */
case '/': /* c is a useless repetitious slash */
c++;
break;
case '\0': /* c is a useless slash at end of path */
c++;
break;
default:
c++; /* c+1 is first char of possible basename */
lastbase = c;
c++; length = 1;
break;
}
} else { /* char in possible basename */
c++; length++;
}
}
if (lastbase == 0) { /* no basename found */
retstr = mymalloc(1);
retstr[0] = '\0';
return retstr;
}
retstr = mymalloc(length+1);
strncpy(retstr, lastbase, length); /* copy only base sans trailing /'s */
retstr[length] = '\0';
return retstr;
}
char*
pathdirname(char* path)
{
char* c;
char* lastslash = 0;
char* retstr;
for (c = path; *c; c++) {
if (*c == '/') {
char* tmplastslash = c;
for (c++; *c == '/'; c++) {;}
/* ignore redundant slashes */
if (*c) { /* not superfluous trailing slash */
lastslash = tmplastslash;
} else { /* was superfluous trailing slash */
break;
}
}
}
if (lastslash == 0) {
if (path[0] == '/') { /* path is "/" or has repeated "/" */
retstr = strdup("/");
return retstr;
} else { /* basename only implies current dir "." */
retstr = strdup(".");
return retstr;
}
}
retstr = mymalloc(lastslash-path+1);
strncpy(retstr, path, lastslash-path);
/* copy only dir sans trailing /'s */
retstr[lastslash-path] = '\0';
return retstr;
}
char*
pathexpandtilde(char* path)
{
char* c;
char user[255];
char* homedir;
char* retstr;
if (path[0] != '~') {
retstr = strdup(path);
return retstr;
}
if (path[1] == '/' || path[1] == '\0') { /* ~/... or ~ */
homedir = getenv("HOME");
c = &(path[1]);
} else { /* ~<user>/... */
struct passwd* pwent;
int i;
c = &(path[1]);
for (i=0; *c != '/' && *c != '\0'; c++, i++) {
user[i] = *c;
}
user[i] = '\0';
pwent = getpwnam(user);
if (pwent == 0) { /* no such user */
retstr = strdup(path);
return retstr;
}
homedir = pwent->pw_dir;
}
if (homedir == 0) {
homedir = ""; /* consistency with csh & ksh */
}
retstr = mymalloc(strlen(homedir) + strlen(c) + 1);
strcpy(retstr, homedir);
strcat(retstr, c);
return retstr;
}